package com.hh.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hh.baseframework.exception.MessageException;
import com.hh.baseframework.util.Convert;
import com.hh.baseframework.util.check.Check;
import com.hh.hibernate.dao.inf.IHibernateDAO;
import com.hh.hibernate.util.dto.HQLParamList;
import com.hh.system.bean.HhXtData;

@Service
public class HhXtDataService {
	@Autowired
	private IHibernateDAO<HhXtData> hhXtDataDAO;

	public List<HhXtData> queryMenuListByPid(String node, String type) {
		HQLParamList hqlParamList = new HQLParamList().addCondition(
				Restrictions.eq("node", node)).addCondition(
				Restrictions.eq("type", type));
		List<HhXtData> hhXtDatas = hhXtDataDAO.queryList(HhXtData.class,
				hqlParamList);
		return hhXtDatas;
	}

	public HhXtData findObjectById(String id) {
		HhXtData hhXtData = hhXtDataDAO.findEntityByPK(HhXtData.class, id);
		return hhXtData;
	}

	public HhXtData save(HhXtData hhXtData) throws MessageException {
		if (checkTextOnly(hhXtData)) {
			throw new MessageException("同级下名称不能一样，请更换！");
		}
		if (Check.isEmpty(hhXtData.getId())) {
			hhXtDataDAO.createEntity(hhXtData);
		} else {
			if (hhXtData.getLeaf() == 1) {
				boolean as = hhXtDataDAO.findWhetherData("select count(o) from " +hhXtData.getClass().getName() + " o "
						+ "where  node = :id", hhXtData);
				if (as) {
					throw new MessageException(
							"此数据已经有子数据，不能修改其为叶子节点，如果要修改请删除子数据！");
				}
			}
			hhXtDataDAO.updateEntity(hhXtData);
		}
		return hhXtData;
	}

	private boolean checkTextOnly(HhXtData hhXtData) {
		return hhXtDataDAO
				.findWhetherData(
						"select count(o) from " +hhXtData.getClass().getName() + " o "
								+ "where o.text=:text and (o.id!=:id or :id is null) and node = :node",
						hhXtData);
	}

	public List<String> deleteByIds(String ids) {
		List<String> idList = Convert.strToList(ids);
		List<String> deleteIdList = new ArrayList<String>();
		deleteYzNode(idList, deleteIdList);
		if (!Check.isEmpty(idList)) {
			hhXtDataDAO.deleteEntity(HhXtData.class, "id", idList);
		}
		return deleteIdList;
	}

	private void deleteYzNode(List<String> idList, List<String> deleteIdList) {
		List<String> yzIdList = new ArrayList<String>();
		if (!Check.isEmpty(idList)) {
			deleteIdList.addAll(idList);
			hhXtDataDAO.deleteEntity(HhXtData.class, "id", idList);
			List<HhXtData> hhxtcdList = hhXtDataDAO.queryList(HhXtData.class,
					Restrictions.in("node", idList));
			for (HhXtData hhXtCd : hhxtcdList) {
				yzIdList.add(hhXtCd.getId());
			}
			deleteYzNode(yzIdList, deleteIdList);
		}
	}

	public List<HhXtData> queryAllMenuListByPid(String type) {
		HQLParamList hqlParamList = new HQLParamList().addCondition(
				Restrictions.eq("node", "root")).addCondition(
				Restrictions.eq("type", type));
		List<HhXtData> hhXtDatas = hhXtDataDAO.queryList(HhXtData.class,
				hqlParamList);
		for (HhXtData hhXtData : hhXtDatas) {
			addYzNode(hhXtData);
		}

		return hhXtDatas;
	}

	private void addYzNode(HhXtData hhXtData) {
		HQLParamList hqlParamList = new HQLParamList().addCondition(
				Restrictions.eq("node", hhXtData.getId())).addCondition(
				Restrictions.eq("type", hhXtData.getType()));
		List<HhXtData> hhXtDatas = hhXtDataDAO.queryList(HhXtData.class,
				hqlParamList);
		hhXtData.setChildren(hhXtDatas);
		for (HhXtData hhXtData2 : hhXtDatas) {
			addYzNode(hhXtData2);
		}
	}
}
